package com.tangding.shiro.support.redis;

import java.util.Collection;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * @description 
 *
 * @author Ranger
 * @version 2017年11月8日_下午8:46:29
 *
 */
@Component
public class StringRedisCacheService {
	
    private static StringRedisTemplate stringRedisTemplate;

    @Autowired
	public void setStringRedisTemplate(StringRedisTemplate stringRedisTemplate) {
		StringRedisCacheService.stringRedisTemplate = stringRedisTemplate;
	}

	public static void set(String key, String value, long time) {
		stringRedisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
    }
	
	public static Set<String> keys(String pattern) {
		return stringRedisTemplate.keys(pattern);
	}
	
	public static String get(String key) {
		return stringRedisTemplate.opsForValue().get(key);
    }
	
	/**
	 * 经过测试只能删除指定的key，不能使模糊删除
	 * 
	 * @param key
	 */
	public static void del(String key) {
		stringRedisTemplate.opsForValue().getOperations().delete(key);
	}
	
	/**
	 * 批量删除key
	 * 
	 * @param keys
	 */
	public static void del(Collection<String> keys) {
		stringRedisTemplate.opsForValue().getOperations().delete(keys);
	}
	
	public static void send2Pub(String topic, String msg) {
		stringRedisTemplate.convertAndSend(topic, msg);
	}
}